package com.itheima.nio.tomcat.domain;

import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

public class HttpRequest {

    private String method;
    private String requestURI;
    private String version;
    private List<Header> headers;
    private SelectionKey selectionKey;
    public HttpRequest(SelectionKey selectionKey){
        this.selectionKey = selectionKey;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getRequestURI() {
        return requestURI;
    }

    public void setRequestURI(String requestURI) {
        this.requestURI = requestURI;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public List<Header> getHeaders() {
        return headers;
    }

    public void setHeaders(List<Header> headers) {
        this.headers = headers;
    }

    public SelectionKey getSelectionKey() {
        return selectionKey;
    }

    public void setSelectionKey(SelectionKey selectionKey) {
        this.selectionKey = selectionKey;
    }

    public void parse(){
        try{
            StringBuilder stringBuilder = new StringBuilder();
            SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            int read = socketChannel.read(byteBuffer);
            while (read > 0){
                byteBuffer.flip();
                byte[] bytes = byteBuffer.array();
                String s = new String(bytes, 0, read);
                stringBuilder.append(s);
                byteBuffer.clear();
                read = socketChannel.read(byteBuffer);
            }

            parseHttpRequest(stringBuilder);

        } catch (Exception e){
            e.printStackTrace();
        }

    }

    public void parseHttpRequest(StringBuilder stringBuilder){
        String httpRequestString = stringBuilder.toString();
        System.out.println("httpRequestString---->" + httpRequestString);
//        httpRequestString.equals("")这样写出现nullPointException
//        if (httpRequestString.equals("") || httpRequestString = null){}
        if ("".equals(httpRequestString) || httpRequestString == null){
            return;
        }
        String[] request = httpRequestString.split("\r\n");
        String httpRequestLine = request[0];
        String[] requestLine = httpRequestLine.split(" ");
        this.method = requestLine[0];
        this.requestURI = requestLine[1];
        this.version = requestLine[2];

        ArrayList<Header> headers = new ArrayList<>();
        for (int i = 1; i < request.length; i++) {
            String requestHeaderInfo = request[i];
            String[] requestHeaderInfoArr = requestHeaderInfo.split(": ");
            Header header = new Header();
            header.setName(requestHeaderInfoArr[0]);
            header.setValue(requestHeaderInfoArr[1]);
            headers.add(header);
        }
        this.headers = headers;
    }
}
